<?php
/**
 * Print recordset
 *
 * @author Arkadiusz Bisaga <abisaga@telaxus.com>
 * @copyright Copyright &copy; 2006, Telaxus LLC
 * @version 1.0
 * @license MIT
 * @package epesi-utils
 * @subpackage RecordBrowser
 */
if(!isset($_REQUEST['cid']) || !isset($_REQUEST['key'])) die('Invalid usage - missing param');
$cid = $_REQUEST['cid'];
$key = $_REQUEST['key'];
$employee = $_REQUEST['employee']; 
$date = $_REQUEST['date'];

define('CID', $cid);
define('READ_ONLY_SESSION',true);
require_once('../../../include.php');

ModuleManager::load_modules();
set_time_limit(0);

$tcpdf = Libs_TCPDFCommon::new_pdf('L');

$employee = Utils_RecordBrowserCommon::get_record('contact', $employee);

$datetime = new DateTime($date);
Libs_TCPDFCommon::prepare_header($tcpdf, 'Phiếu giao việc', 'Ngày: ' . $datetime->format('d/m/Y') . '        Nhân viên: ' . $employee['last_name'] . ' ' . $employee['first_name'] , false);

Libs_TCPDFCommon::SetFont($tcpdf, Libs_TCPDFCommon::$default_font, '', 13);

$count = 0;
$manager =  CRM_ContactsCommon::get_my_record();
$recs = SCTV_RequirementCommon::get_requirements_by_technical($date, $employee['id'], $manager['id'], false);

foreach($recs as $key=>$value)
{
	if($count%20 == 0)
	{
		Libs_TCPDFCommon::add_page($tcpdf);
		$html = '<ul>';
	}
	$html .= '<li>';
	
	$services = SCTV_RequirementCommon::get_cable_status_name($value['cable_status']);
			if($value['number_of_hd'] > 0) $services .= ' ' . $value['number_of_hd'] . 'HD';
			if($value['number_of_sd'] > 0) $services .= ' ' . $value['number_of_sd'] . 'SD';
            if($value['main_analog'] > 0) $services .= ' ' . $value['main_analog'] . 'MAL';
            if($value['sub_analog'] > 0) $services .= ' ' . $value['sub_analog'] . 'SAL';
			if($value['internet_package'] != NULL) $services .= ' Net:' . $value['internet_package'];
			if($value['bundle'] != NULL) $services .= ' Trọn gói:' . $value['bundle'];
			 
	$html .='#' . $value['requirement_code'] . ' ' . $value['customer_name'] . '(' . $value['address'] . ' ' . $value['street'] . '[' . $value['area'] . ']'. ', ĐT: ' . $value['phone'] . ')(' . $services . ')';
	if($value['note'] != null)
		$html .='. Ghi chú: ' . $value['note'];
	if($value['charge'] > 0)
		$html .='. Chốt cước nóng: ' . Utils_CurrencyFieldCommon::format($value['charge']);
				
	$html .= '</li>';
	
	$count = $count + 1;
	if($count%20 == 0)
	{
		$html .= '</ul>';		
		$html = Libs_TCPDFCommon::stripHTML(str_replace(array('<br>','&nbsp;'),array('<br/>',' '),$html));
		Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);		
	}
}
if($count%20 > 0)
{
	$html .= '</ul>';

	$html = Libs_TCPDFCommon::stripHTML(str_replace(array('<br>','&nbsp;'),array('<br/>',' '),$html));
	Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);	
}

$buffer = Libs_TCPDFCommon::output($tcpdf);

header('Content-Type: application/pdf');
header('Content-Length: '.strlen($buffer));
header('Content-disposition: inline; filename="recordset_'.$tab.'.pdf"');

print($buffer);

?>
